# -*- shell-script -*-

# 41rtas_get_ibm_vpd - Fetch/update dynamic VPD on IBM pSeries when available.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2004, 2005

# Maintained by Martin Schwenke <martins@au.ibm.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
# $Id: 41rtas_get_ibm_vpd,v 1.1 2006/04/11 18:38:28 emunson Exp $

rtas_ibm_get_vpd=$(type -p rtas_ibm_get_vpd)

[ -f "${db_bus_dt_dir}/rtas/ibm,get-vpd" -a -n "$rtas_ibm_get_vpd" ] || \
    return 0

######################################################################

dynamic_vpd_hooks="${dynamic_vpd_hooks} rtas_ibm_get_vpd_hook"

rtas_ibm_get_vpd_hook ()
{
    if can_cache_vpd ; then

	# FIXME: Could make this a symlink-based "method" and push the
	# work out to an external program.  However, this is pretty
	# simple.

	# Planning to remove old VPD.  Be very, very careful...
	local d="$db_bus_dt_dir"
	case "$d" in
	    (/*/${db_bus_dt_subdir}) : ;; # Sanity check.
	    *)
		: "Problem with dynamic VPD node: \"${d}\""
		return
	esac
	
	local t="${d}/${linux_dynamic_vpd}"
	local l="${d}/linux,vpd"
	
	[ -e "$l" -o -e "$t" ] && rm -rf "$l" "$t"

	ensure_directory "$d"

	if lock_file "$t" ; then
	    if debug_cmd $rtas_ibm_get_vpd >"$t"  ; then
		debug_cmd ibm_vpd_render "$t" "$d"
		local fc
		do_fc_hook "$d"
		vpd_subdirs_list_hook "$d"
		local vpd_dir
		for vpd_dir in $vpd_subdirs ; do
		    vpd_field_override "$vpd_dir" "FC" "$fc"
		done
	    else
		# Don't want to keep an empty file on failure.  The
		# alternative would be to put the output into a
		# temporary file and move it over on success, allowing
		# any old data to be kept on failure.  However, we
		# don't want stale data.
		rm -f "$t"
	    fi
	    unlock_file "$t"
	fi
    fi
}
